using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Sys.DataAccess;

namespace Pervote.Core.Logic
{
    public class CategoryManager
    {
        private static DatabaseCommand.CreateCommand commandCreator = DatabaseCommand.GetCreateCommand(PervoteSettings.ConnectionString, DatabaseType.SqlServer);

        public DataTable GetAllCategories()
        {
            DatabaseCommand command = commandCreator("PV_SP_GetAllCategories", CommandType.StoredProcedure, false);            
            DataTable categories = command.GetResultsAsTable();
            return categories;
        }

        public long CreateCategory(Category category, ErrorInfo error)
        {
            DatabaseCommand command = commandCreator("PV_SP_CreateCategory", CommandType.StoredProcedure, false);
            command.AddInParameter("Name", category.Name);
            command.AddInParameter("Description", category.Description);
            command.AddInParameter("CreatedBy", category.CreatedBy);
            command.AddInParameter("ParentId", category.ParentId);
            command.AddOutputParameter("CategoryId", DbType.Int64);
            long catId;
            error.ErrorCode = (int)command.ExecuteScalar();
            catId = (long)command.GetParameterValue("CategoryId");
            return catId;
        }

        public DataTable GetRootCategories()
        {
            return GetAllCategories();
        }
    }
}
